package com.feilong.edgeAI.view;


import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

import java.util.List;

public abstract class BaseSurfaceView extends SurfaceView implements Callback {
    private static final String TAG = "RenderView";
    public float mVolume;
    private final Object surfaceLock;
    private RenderThread renderThread;
    private List<IRenderer> renderer;

    public BaseSurfaceView(Context context) {
        this(context, (AttributeSet)null);
    }

    public BaseSurfaceView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BaseSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.surfaceLock = new Object();
        this.getHolder().addCallback(this);
        this.setZOrderOnTop(true);
        this.getHolder().setFormat(-2);
    }

    public void setVolume(float volume) {
        volume *= volume;
//        Log.d("RenderView", "setVolume() called with: volume = [" + volume + "]");
        if (this.mVolume > volume) {
            float diff = this.mVolume - volume;
            float v = diff / 8.0F;
            this.mVolume -= v;
        } else {
            this.mVolume = volume;
        }

    }

    public void surfaceCreated(SurfaceHolder holder) {
        this.renderer = this.onCreateRenderer();
        if (this.renderer != null && this.renderer.isEmpty()) {
            throw new IllegalStateException();
        } else {
            this.renderThread = new RenderThread(holder);
            this.renderThread.start();
        }
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        synchronized(this.surfaceLock) {
            this.renderThread.setRun(false);
        }
    }

    protected List<IRenderer> onCreateRenderer() {
        return null;
    }

    private void render(Canvas canvas, float volume) {
        if (this.renderer != null) {
            int i = 0;

            for(int size = this.renderer.size(); i < size; ++i) {
                ((IRenderer)this.renderer.get(i)).onRender(canvas, volume);
            }
        } else {
            this.onRender(canvas, volume);
        }

    }

    protected void onRender(Canvas canvas, float volume) {
    }

    public interface IRenderer {
        void onRender(Canvas var1, float var2);
    }

    private class RenderThread extends Thread {
        private static final long SLEEP_TIME = 16L;
        private SurfaceHolder surfaceHolder;
        private boolean running = true;

        public RenderThread(SurfaceHolder holder) {
            super("RenderThread");
            this.surfaceHolder = holder;
        }

        public void run() {
            while(true) {
                synchronized(BaseSurfaceView.this.surfaceLock) {
                    if (!this.running) {
                        return;
                    }

                    Canvas canvas = this.surfaceHolder.lockCanvas();
                    if (canvas != null) {
                        BaseSurfaceView.this.render(canvas, BaseSurfaceView.this.mVolume);
                        this.surfaceHolder.unlockCanvasAndPost(canvas);
                    }
                }

                try {
                    Thread.sleep(5L);
                } catch (InterruptedException var4) {
                    var4.printStackTrace();
                }
            }
        }

        public void setRun(boolean isRun) {
            this.running = isRun;
        }
    }
}
